package com.fly.lambda.controller;

import com.fly.lambda.domain.Config;
import com.fly.lambda.framework.dao.Pagination;
import com.fly.lambda.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fly on 2016/7/28.
 */
@Controller
@RequestMapping("/manage/config")
public class ConfigCtrl {

    @Autowired
    ConfigService configService;

    @RequestMapping("/list")
    public String toConfigList(
            ModelMap model
    ){
        return "/manage/config/list";
    }

    @RequestMapping("/getData")
//    @ResponseBody
    public String getConfigData(
            @RequestParam(value ="s", required = false, defaultValue = "")String queryStr,
            @RequestParam(value = "curPage", required = false, defaultValue = "1")int curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "15") int pageSize,
            ModelMap model
    )
    {
        Pagination<Config> configPage = configService.queryConfigPage(queryStr, curPage, pageSize);
        model.addAttribute("configPage", configPage);
        return "/manage/config/configData";
    }

    @RequestMapping("/form")
    public String toForm(
            @RequestParam(value = "configId", required = false, defaultValue = "")String configId,
            ModelMap model
    ){
        Config config = configService.getConfigById(configId);
        model.addAttribute("config", config);
        return "/manage/config/form";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, String> saveConfig(
            @RequestParam(value = "configId", required = false, defaultValue = "")String configId,
            @RequestParam(value = "configName", required = false, defaultValue = "")String configName,
            @RequestParam(value = "configValue", required = false, defaultValue = "")String configValue,
            @RequestParam(value = "description", required = false, defaultValue = "")String description
    ){
        Map<String, String> map = new HashMap<String, String>();
        Config config = new Config();
        config.setConfigId(configId);
        config.setConfigName(configName);
        config.setConfigValue(configValue);
        config.setDescription(description);
        configService.saveConfig(config);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("refresh")
    @ResponseBody
    public Map<String, String> refreshConfig(){
        Map<String, String> map = new HashMap<String, String>();
        configService.initConfig();
        map.put("result", "success");
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "configId", required = false, defaultValue = "")String configId
    ){
        Map<String, String> map = new HashMap<String, String>();
        configService.deleteConfigById(configId);
        map.put("result", "success");
        return map;
    }

}
